package br.com.oficicar.integracao;

import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.oficicar.entidades.Modulo;
import br.com.oficicar.entidades.Perfil;

@Repository
@Scope("session")
public class ModuloDAO extends DAOGenerico<Modulo> implements CRUD<Modulo> {
	//@Transactional(readOnly=true)
	public Collection<Modulo> getModulosPerfil(Perfil perfil) {
		Criteria ct = getSession().createCriteria(Perfil.class);
		ct.createAlias("permissao", "permissao");
		ct.createAlias("permissao.modulo", "modulo");
		ct.add(Restrictions.idEq(perfil.getId()));
		ct.setProjection(Projections.distinct(Projections.property("permissao.modulo")));
		return ct.list();
	}
	
	//@Transactional(readOnly=true)
	public boolean temFilhos(Modulo modulo) {
		Criteria ct = criarCriteria();
		ct.add(Restrictions.eq("pai", modulo));
		ct.setProjection(Projections.count("id"));
		return ((Long)ct.uniqueResult())>0L?true:false;
	}
	
	//@Transactional(readOnly=true)
	public Collection<Modulo> getFilhos(Modulo modulo){
		Criteria ct = criarCriteria();
		ct.add(Restrictions.eq("pai", modulo));
		return ct.list();
	}
	
}
